package com.example.springdemo.handlers;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.springdemo.enums.Status;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * ResHandler
 * 统一返回格式处理器
 *
 * @author wenguang
 * @date 2021/11/11 17:04
 */
@Slf4j
public class MpMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("start insert fill ....");
        LocalDateTime now = LocalDateTime.now();
        Object createTime = this.getFieldValByName("createTime", metaObject);
        if (Objects.isNull(createTime)) {
            this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, now);
        }

        Object updateTime = this.getFieldValByName("updateTime", metaObject);
        if (Objects.isNull(updateTime)) {
            this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, now);
        }

        Object status = this.getFieldValByName("status", metaObject);
        if (Objects.isNull(status)) {
            this.strictInsertFill(metaObject, "status", Status.class, Status.VALID);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("start update fill ....");
    }

}
